import React, { Component } from 'react'
import Header from '../../components/Header/Header'
import Banner from "./components/banner/banner"
import News from './components/news/news'
import Seckill from "./components/seckill/seckill"
import Goods from "./components/goods/goods"
import { reqgetbanner, reqgetindexgoods, reqgetnew, reqgetseckill } from '../../http/api'



export default class home extends Component {
  constructor() {
    super()
    this.state = {
      banner: [],
      news: [],
      seckill: [],
      goods: [],
      n: 0,
    }
  }

  componentDidMount() {
    this.getbanner();
    this.getnew();
    this.getseckil();
    this.getGoods();
  }
  //  请求轮播数据
  async getbanner() {
    let res = await reqgetbanner()
    if (res.data.code === 200) {
      this.setState({
        banner: res.data.list
      })
    }
  }
  // 新人专享
  async getnew() {
    let res = await reqgetnew();
    if (res.data.code === 200) {
      this.setState({
        news: res.data.list
      })
    }
  }

  // 秒杀

  async getseckil() {
    let res = await reqgetseckill();
    if (res.data.code === 200) {
      this.setState({
        seckill: res.data.list
      })
    }
  }

  // 商品
  async getGoods() {
    let res = await reqgetindexgoods();
    if (res.data.code === 200) {
      this.setState({
        goods: res.data.list ? res.data.list : []
      });
    }
  }

  render() {
    let { banner, news, seckill, goods, n } = this.state
    return (
      <div>
        <Header title="首页"></Header>

        {/* 轮播 */}
        <Banner banner={banner}></Banner>
        {/* 新人专享 */}
        <News news={news}></News>
        {/* {秒杀} */}
        <Seckill seckill={seckill}></Seckill>
        {/* 首页商品 */}
        <Goods goods={goods} n={n} onchange={n => {
          this.setState({
            n
          })
        }}></Goods>
      </div>
    )
  }
}
